package com.bib.ciglog;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bib.ciglog.data.CigObj;
import com.bib.ciglog.data.DaoProvider;

public class CigArrayAdapter extends ArrayAdapter<CigObj> {

	private CigArrayAdapter cigArray = null;
	private Context context;
	private TextView textView = null;
	private ImageView deleteImageView = null;

	public CigArrayAdapter(Context context, List<CigObj> values) {
		super(context, R.layout.list_item, values);
		this.cigArray = this;
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.list_item, parent, false);

		textView = (TextView) view.findViewById(R.id.cigItem);
		deleteImageView = (ImageView) view.findViewById(R.id.imgDelCig);
		System.out.println(position);
		final int i = position;
		final CigObj cig = this.getItem(position);
		textView.setText(cig.toString());

		deleteImageView.setImageResource(R.drawable.ic_action_discard);



		deleteImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				new AlertDialog.Builder(context)
			    .setTitle("Warn")
			    .setMessage("Are you sure you want to remove this cig? \\n" + cig.toShortString())
			    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int which) { 
			        	DaoProvider.getData().clearData();
			    		notifyDataSetChanged();
			        }
			     })
			    .setNegativeButton("No", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int which) { 
			            // do nothing
			        }
			     })
			     .show();
				
			}
		});

		return view;
	}
}